home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyo (Python 2.5)
-
- DISABLE_ALL_CACHING = False
- NUM_IMAGES_TO_CACHE = 80
- from util import try_this
- from util.lrucache import LRU
- import wx
- from wx import ImageFromDataWithAlpha, BitmapFromImage, Bitmap, BitmapFromIcon, ImageFromBitmap, IMAGE_QUALITY_HIGH, Point
- from PIL import Image, ImageDraw, ImageFilter, ImageMath, ImageFont, ImageOps, ImageEnhance
- from PIL.Image import BICUBIC, ANTIALIAS
- from functools import wraps
- from uuid import uuid1
- imagecache = LRU(NUM_IMAGES_TO_CACHE)
- CORNERS = (('tl', None), ('tr', Image.ROTATE_270), ('bl', Image.FLIP_TOP_BOTTOM), ('br', Image.ROTATE_180))
-
- def cacheing(func):
- name = func.__name__
-
- def wrapper(img, *a):
-
- try:
- cachekey = img.cachekey
- except AttributeError:
-
- try:
- cachekey = (img.path,)
- except AttributeError:
- img.path = str(uuid1())
- cachekey = (img.path,)
- except:
- None<EXCEPTION MATCH>AttributeError
-
-
- None<EXCEPTION MATCH>AttributeError
-
- key = cachekey + (name + repr(a),)
-
- try:
- return imagecache[key]
- except KeyError:
- img = func(img, *a)
- img.cachekey = key
- imagecache[key] = img
- return img
-
-
- wrapper = (None, wraps(func))(wrapper)
- return wrapper
-
- if DISABLE_ALL_CACHING:
- import sys
- print >>sys.stderr, 'WARNING: image caching is disabled'
- objmemoize = refmemoize = cacheing = (lambda f: f)
- else:
- refmemoize = objmemoize = cacheing
-
- def wxbitmap_to_wximage(wxbitmap):
- return wxbitmap.ConvertToImage()
-
- wxbitmap_to_wximage = cacheing(wxbitmap_to_wximage)
-
- def wxbitmap_to_pil(wxbitmap):
- return wximage_to_pil(wxbitmap_to_wximage(wxbitmap))
-
- wxbitmap_to_pil = refmemoize(wxbitmap_to_pil)
-
- def wxicon_to_pil(wxicon):
- return wxbitmap_to_pil(BitmapFromIcon(wxicon))
-
- wxicon_to_pil = cacheing(wxicon_to_pil)
-
- def wxicon_to_bitmap(wxicon):
- return BitmapFromIcon(wxicon)
-
- wxicon_to_bitmap = cacheing(wxicon_to_bitmap)
-
- def wximage_to_pil(wximage):
- size = (wximage.Width, wximage.Height)
- img = Image.new('RGB', size)
- img.fromstring(wximage.Data)
- (r, g, b) = img.split()
- if wximage.HasAlpha() or wximage.HasMask():
- a = Image.new('L', wximage.GetSize())
- if wximage.HasMask():
- if not wximage.HasAlpha():
- wximage.InitAlpha()
-
-
- if wximage.HasAlpha():
- a.fromstring(wximage.AlphaData)
-
- img = Image.merge('RGBA', (r, g, b, a))
- else:
- img = Image.merge('RGB', (r, g, b))
- return img
-
- wximage_to_pil = refmemoize(wximage_to_pil)
-
- def wximage_to_wxbitmap(wximage, depth = 32):
- return BitmapFromImage(wximage)
-
-
- def pil_to_wxbitmap(pilimg):
- return wximage_to_wxbitmap(pil_to_wximage(pilimg), 32)
-
- pil_to_wxbitmap = objmemoize(pil_to_wxbitmap)
-
- def pil_to_wximage(pilimg):
- pilimage = None if pilimg.mode == 'RGBA' else pilimg.convert('RGBA')
- (w, h) = pilimg.size
- rgb = pilimage.tostring('raw', 'RGB')
- alpha = pilimage.tostring('raw', 'A')
- return ImageFromDataWithAlpha(w, h, rgb, alpha)
-
- pil_to_wximage = objmemoize(pil_to_wximage)
-
- def wxbitmap_inverted(wxbitmap):
- return ImageOps.invert(pilimg_convert(wxbitmap.PIL, 'RGB')).WXB
-
- wxbitmap_inverted = cacheing(wxbitmap_inverted)
-
- def has_transparency(i):
- return any(i.histogram()[768:1024])
-
-
- def pil_to_white_gif(i):
- bg = Image.new('RGBA', i.size, (255, 255, 255, 255))
- bg.paste(i, i)
- return pil_to_gif(bg)
-
-
- def pil_to_gif(i):
- (r, g, b, a) = i.split()
- rgb = Image.merge('RGB', (r, g, b))
- rgb = rgb.convert('P', palette = Image.ADAPTIVE, colors = 255, dither = Image.NONE)
- pal = rgb.getpalette()
- r1 = pal[::3]
- g1 = pal[1::3]
- b1 = pal[2::3]
- rv = (set(range(256)) - set(r1)).pop()
- gv = (set(range(256)) - set(g1)).pop()
- bv = (set(range(256)) - set(b1)).pop()
- pal[-3:] = [
- rv,
- gv,
- bv]
- rgb.putpalette(pal, rawmode = 'RGB')
- a2 = a.point((lambda p: None if p >= 128 else 255))
- rgb.paste(255, (0, 0), a2)
- import cStringIO
- s = cStringIO.StringIO()
- rgb.save(s, 'GIF', transparency = 255, interlace = False)
- return s.getvalue()
-
-
- def wxbitmap_resizedwh(wxbitmap, w, h):
- return pil_to_wxbitmap(pilimg_resizedwh(wximage_to_pil(wxbitmap_to_wximage(wxbitmap)), w, h))
-
- wxbitmap_resizedwh = refmemoize(wxbitmap_resizedwh)
-
- def wxbitmap_resized(wxbitmap, size):
-
- try:
- (w, h) = size
- except TypeError:
- return wxbitmap_in_square(wxbitmap, size)
-
- return wxbitmap_resizedwh(wxbitmap, w, h)
-
- wxbitmap_resized = refmemoize(wxbitmap_resized)
-
- def wxbitmap_resized_smaller(wxbitmap, size):
- if max(wxbitmap.Width, wxbitmap.Height) > size:
- return wxbitmap.Resized(size)
- else:
- return wxbitmap
-
-
- def pil_resized_smaller(pil, size):
- return None if max(pil.size) > size else pil
-
-
- def pilimg_resized(pilimg, size):
-
- try:
- (w, h) = size
- except TypeError:
- return pilimg_in_square(pilimg, size)
-
- return pilimg_resizedwh(pilimg, w, h)
-
- pilimg_resized = objmemoize(pilimg_resized)
-
- def pilimg_convert(pilimg, mode):
- return pilimg.convert(mode)
-
- pilimg_convert = objmemoize(pilimg_convert)
-
- def pilimg_resizedwh(pilimg, w, h):
- w = None if w == -1 else int(w)
- h = None if h == -1 else int(h)
- size = (w, h)
- if pilimg.mode == 'P':
- pilimg = pilimg_convert(pilimg, 'RGBA')
-
- return None(pilimg.resize, size if min(size) < min(pilimg.size) else Image.BICUBIC)
-
- pilimg_resizedwh = objmemoize(pilimg_resizedwh)
-
- def pilimg_in_square(img, squaresize):
- (w, h) = img.size
- if img.mode == 'P':
- img = pilimg_convert(img, 'RGBA')
-
- new = Image.new('RGBA', (squaresize, squaresize), (0, 0, 0, 0))
- if w > h:
- new_height = int((h / float(w)) * squaresize)
- img = None(img.resize, (squaresize, new_height) if squaresize > w else ANTIALIAS)
- new.paste(img, (0, (squaresize - new_height) / 2))
- else:
- new_width = int((w / float(h)) * squaresize)
- img = None(img.resize, (new_width, squaresize) if squaresize > h else ANTIALIAS)
- new.paste(img, ((squaresize - new_width) / 2, 0))
- return new
-
- pilimg_in_square = objmemoize(pilimg_in_square)
-
- def pil_resize_canvas(img, w, h, alignment = wx.ALIGN_CENTER):
- if alignment != wx.ALIGN_CENTER:
- raise NotImplemented
-
- (ow, oh) = img.size
- new = Image.new('RGBA', (w, h), (0, 0, 0, 0))
- new.paste(img, (w / 2 - ow / 2, w / 2 - oh / 2))
- return new
-
- pil_resize_canvas = objmemoize(pil_resize_canvas)
-
- def pil_setalpha(img, alpha):
- channels = img.split()
- if len(channels) == 4:
- (r, g, b, a) = channels
- elif len(channels) == 3:
- a = Image.new('L', img.size, 'white')
- else:
- raise AssertionError('Cannot set alpha, image does not have 3 or 4 bands.')
- img.putalpha(ImageEnhance.Brightness(a).enhance(alpha))
-
-
- def wxbitmap_in_square(bmp, squaresize, scaleup = True):
- w = bmp.Width
- h = bmp.Height
- if w > squaresize and h > squaresize or scaleup:
- img = ImageFromBitmap(bmp)
- if w > h:
- new_height = int((h / float(w)) * squaresize)
- img = img.Scale(squaresize, new_height, IMAGE_QUALITY_HIGH)
- offset = Point(0, (squaresize - new_height) / 2)
- else:
- new_width = int((w / float(h)) * squaresize)
- img = img.Scale(new_width, squaresize, IMAGE_QUALITY_HIGH)
- offset = Point((squaresize - new_width) / 2, 0)
- return BitmapFromImage(img)
-
- return bmp
-
- wxbitmap_in_square = refmemoize(wxbitmap_in_square)
-
- def wxbitmap_greyed(bitmap):
- return wximage_to_wxbitmap(wxbitmap_to_wximage(bitmap).ConvertToGreyscale())
-
- wxbitmap_greyed = refmemoize(wxbitmap_greyed)
-
- def pilimg_greyed(pil):
- pil = pil.copy()
- alpha = 'A' in pil.getbands()
- if alpha:
- (r, g, b, a) = pil.split()
-
- pil = ImageOps.grayscale(pil)
- if alpha:
- pil.putalpha(a)
-
- return pil
-
- pilimg_greyed = objmemoize(pilimg_greyed)
-
- def drop_shadow(image, offset = (5, 5), background = (0, 0, 0, 0), shadow = 4473924, border = 8, iterations = 3):
- image = image.PIL
- totalWidth = image.size[0] + abs(offset[0]) + 2 * border
- totalHeight = image.size[1] + abs(offset[1]) + 2 * border
- back = Image.new(image.mode, (totalWidth, totalHeight), background)
- shadowLeft = border + max(offset[0], 0)
- shadowTop = border + max(offset[1], 0)
- back.paste(shadow, [
- shadowLeft,
- shadowTop,
- shadowLeft + image.size[0],
- shadowTop + image.size[1]])
- n = 0
- while n < iterations:
- back = back.filter(ImageFilter.BLUR)
- n += 1
- imageLeft = border - min(offset[0], 0)
- imageTop = border - min(offset[1], 0)
- back.paste(image, (imageLeft, imageTop))
- return back
-
-
- def pil_combine(imgs, direction = wx.HORIZONTAL, align = wx.ALIGN_CENTER):
- imgs = [ img.PIL for img in imgs ]
- length_idx = [
- wx.HORIZONTAL,
- wx.VERTICAL].index(direction)
- breadth_idx = [] if length_idx else 1
- for img in imgs:
- size = img.size
- length += size[length_idx]
- breadth = max(breadth, size[breadth_idx])
-
- newsize = length = breadth = 0 if direction == wx.HORIZONTAL else (breadth, length)
- newimg = Image.new('RGBA', newsize, (0, 0, 0, 0))
- pos = (0, 0)
- for img in imgs:
- newimg.paste(img, pos)
- if direction == wx.HORIZONTAL:
- pos = (pos[0] + img.size[0], pos[1])
- continue
- []
- pos = (pos[0], pos[1] + img.size[1])
-
- return newimg
-
-
- def rounded_corners(corner_size = 1, rounded_imgs = { }):
- if not isinstance(corner_size, int):
- corner_size = (try_this,)((lambda : int(bool(corner_size))), 1)
- else:
- corner_size = max(0, min(corner_size, 3))
-
- try:
- return rounded_imgs[corner_size]
- except KeyError:
- pass
-
- imgs = []
- skin = skin
- import gui
- rounded_img = Image.open(skin.resourcedir() / ('corner' + str(corner_size) + '.gif')).convert('L')
- for name, rotation in CORNERS:
- mask = None if rotation is not None else rounded_img
- imgs.append(mask)
-
- return rounded_imgs.setdefault(corner_size, imgs)
-
-
- def rounded_mask(size, cornersize = 1):
- img = Image.new('L', size, 255)
- (w, h) = size
- p = img.paste
- r = rounded_corners(cornersize)
- i = r[0]
- p(i, (0, 0, i.size[0], i.size[1]))
- i = r[1]
- p(i, (w - i.size[0], 0, w, i.size[1]))
- i = r[2]
- p(i, (0, h - i.size[1], i.size[0], h))
- i = r[3]
- p(i, (w - i.size[0], h - i.size[1], w, h))
- return img
-
-
- def pilimg_rounded(image, cornersize = 1):
- newimage = image.copy()
- newimage.putalpha(ImageMath.eval('convert(min(a,b), "L")', a = newimage.split()[-1], b = rounded_mask(newimage.size, cornersize)))
- return newimage
-
- pilimg_rounded = objmemoize(pilimg_rounded)
-
- def wxbitmap_rounded(wxbitmap, cornersize = 1):
- if cornersize == 0:
- return wxbitmap
- else:
- return pil_to_wxbitmap(pilimg_rounded(wximage_to_pil(wxbitmap_to_wximage(wxbitmap)), cornersize))
-
- wxbitmap_rounded = refmemoize(wxbitmap_rounded)
- Image.Image.Resized = pilimg_resized
- Image.Image.ResizedSmaller = pil_resized_smaller
- Image.Image.Rounded = pilimg_rounded
- Image.Image.WXB = property((lambda pil: pil_to_wxbitmap(pil)))
- Image.Image.WX = property((lambda pil: pil_to_wximage(pil)))
- Image.Image.Greyed = property(pilimg_greyed)
- Image.Image.ResizeCanvas = pil_resize_canvas
- Image.Image.PIL = property((lambda pil: pil))
- wx.Bitmap.Resized = wxbitmap_resized
- wx.Bitmap.ResizedSmaller = wxbitmap_resized_smaller
- wx.Bitmap.Greyed = property(wxbitmap_greyed)
- wx.Bitmap.Rounded = wxbitmap_rounded
- wx.Bitmap.WXB = property((lambda self: self))
- wx.Bitmap.WX = property((lambda wxb: wxbitmap_to_wximage(wxb)))
- wx.Bitmap.PIL = property((lambda self: wxbitmap_to_pil(self)))
- wx.Bitmap.Inverted = property(wxbitmap_inverted)
- wx.Image.WX = property((lambda self: self))
- wx.Image.WXB = property((lambda self: wximage_to_wxbitmap(self, 32)))
- wx.Image.PIL = property((lambda self: wximage_to_pil(self)))
- wx.Icon.PIL = property((lambda self: wxicon_to_pil(self)))
- wx.Icon.WXB = property((lambda self: wxicon_to_bitmap(self)))
- if __name__ == '__main__':
- from tests.testapp import testapp
- from gui.skin import get
- a = testapp('../../..')
- img = Image.new('RGBA', (40, 50), 'red')
- img.Resized(100).Show()
- a.MainLoop()
-
-